﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Dora.Tools.Utility
{
  public class DateHelper
    {
        /// <summary>
        /// 根据当前时间返回 本日 本周 本月 本季 本年 
        /// </summary>
        /// <returns></returns>
        public static string[] GetDataByNow(int type=1)
        {
            string[] res=new string[2];
            DateTime dt = DateTime.Now;
            switch (type)
            {
                case 0:
                    res[0] = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    res[1] = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                case 1:
                    DateTime BeginWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一
                    DateTime EndWeek = BeginWeek.AddDays(6); 
                    res[0] = BeginWeek.ToString("yyyy-MM-dd");
                    res[1] = EndWeek.ToString("yyyy-MM-dd");
                    break;
                case 2:
                    DateTime BeginMonth = dt.AddDays(1 - dt.Day);  
                    DateTime EndMonth = BeginMonth.AddMonths(1).AddDays(-1);
                    res[0] = BeginMonth.ToString("yyyy-MM-dd");
                    res[1] = EndMonth.ToString("yyyy-MM-dd");
                    break;
                case 3:
                    DateTime BeginQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);
                    DateTime EndQuarter = BeginQuarter.AddMonths(3).AddDays(-1);
                    res[0] = BeginQuarter.ToString("yyyy-MM-dd");
                    res[1] = EndQuarter.ToString("yyyy-MM-dd");
                    break;
                case 4:
                    DateTime BeginYear = new DateTime(dt.Year, 1, 1); 
                    DateTime EndYear = new DateTime(dt.Year, 12, 31); 
                    res[0] = BeginYear.ToString("yyyy-MM-dd");
                    res[1] = EndYear.ToString("yyyy-MM-dd");
                    break;
            }
            return res;
        }

        /// <summary>
        /// 转换英文缩写月份为数字月份
        /// </summary>
        /// <param name="monthEnAbbr">英文缩写月份</param>
        /// <returns></returns>
        public static int MonthFromEnAbbrToNum(string monthEnAbbr)
        {
            var month = 0;
            if (string.IsNullOrEmpty(monthEnAbbr))
            {
                return month;
            }
            switch (monthEnAbbr.ToUpper())
            {
                case "JAN":
                    month = 1;
                    break;
                case "FEB":
                    month = 2;
                    break;
                case "MAR":
                    month = 3;
                    break;
                case "APR":
                    month = 4;
                    break;
                case "MAY":
                    month = 5;
                    break;
                case "JUN":
                    month = 6;
                    break;
                case "JUL":
                    month = 7;
                    break;
                case "AUG":
                    month = 8;
                    break;
                case "SEP":
                    month = 9;
                    break;
                case "OCT":
                    month = 10;
                    break;
                case "NOV":
                    month = 11;
                    break;
                case "DEC":
                    month = 12;
                    break;
                default:
                    break;
            }
            return month;
        }

        /// <summary>
        /// 转换数字月份为英文缩写月份
        /// </summary>
        /// <param name="monthNum">英文缩写月份</param>
        /// <returns></returns>
        public static string GetMonthEnAbbrFromNum(int monthNum)
        {
            var month = "";
            if (monthNum<1&&monthNum>12)
            {
                return month;
            }
            switch (monthNum)
            {
                case 1:
                    month = "JAN";
                    break;
                case 2:
                    month = "FEB";
                    break;
                case 3:
                    month = "MAR";
                    break;
                case 4:
                    month = "APR";
                    break;
                case 5:
                    month = "MAY";
                    break;
                case 6:
                    month = "JUN";
                    break;
                case 7:
                    month = "JUL";
                    break;
                case 8:
                    month = "AUG";
                    break;
                case 9:
                    month ="SEP" ;
                    break;
                case  10:
                    month ="OCT";
                    break;
                case 11:
                    month = "NOV";
                    break;
                case 12:
                    month = "DEC";
                    break;
                default:
                    break;
            }
            return month;
        }

        /// <summary>
        /// 根据传入时间转换为时间戳
        /// </summary>
        /// <param name="DateTime"></param>
        /// <returns></returns>
        public static long GetTimeStamp(string IputDateTime)
        {
            DateTime input = new DateTime();
            if (DateTime.TryParse(IputDateTime, out input))
            {
                DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
                long timeStamp = Convert.ToInt32((input - dateStart).TotalSeconds);
                return timeStamp;
            }
            return 0;
        }

    }
}
